
all: prepare build-packages build-dockers

# Stage 0
prepare: config-maven config-npm

config-maven:
	mkdir -pv $(HOME)/.m2
	install -m644 docs/settings.xml $(HOME)/.m2

config-npm:
	install -m644 docs/npmrc $(HOME)/.npmrc

docker-pull-external-images:
	docker pull registry.cn-qingdao.aliyuncs.com/metersphere/alpine-openjdk17-jre
	docker pull registry.cn-qingdao.aliyuncs.com/metersphere/jmeter-master:5.5-ms12-jdk17
	docker pull registry.cn-qingdao.aliyuncs.com/metersphere/kafka:3.7.0
	docker pull registry.cn-qingdao.aliyuncs.com/metersphere/minio:RELEASE.2023-04-13T03-08-07Z
	docker pull registry.cn-qingdao.aliyuncs.com/metersphere/mysql:8.0.35
	docker pull registry.cn-qingdao.aliyuncs.com/metersphere/node-chromium:4.10.0
	docker pull registry.cn-qingdao.aliyuncs.com/metersphere/node-firefox:4.10.0
	docker pull registry.cn-qingdao.aliyuncs.com/metersphere/selenium-hub:4.10.0

define docker-pull-image
	docker pull registry.cn-qingdao.aliyuncs.com/metersphere/$(1):v2.10.20-lts
endef

docker-pull-release-images:
	$(call docker-pull-image,api-test)
	$(call docker-pull-image,eureka)
	$(call docker-pull-image,gateway)
	$(call docker-pull-image,performance-test)
	$(call docker-pull-image,project-management)
	$(call docker-pull-image,report-stat)
	$(call docker-pull-image,test-track)
	$(call docker-pull-image,system-setting)
	$(call docker-pull-image,workstation)


# Stage 1
build-packages:
	./mvnw install -N
	./mvnw clean package

# Stage 1.1
# Build in docker, with openjdk 1.17
build-packages-in-docker:
	sudo docker run -it --user 1000:1000 -v $(HOME):$(HOME) -w $(PWD) \
		registry.cn-qingdao.aliyuncs.com/metersphere/alpine-openjdk17-jre \
		bash docs/build-in-docker.sh

## Stage 2
build-dockers: \
	docker-api-test \
	docker-framework-eureka \
	docker-framework-gateway \
	docker-performance-test \
	docker-report-stat \
	docker-test-track \
	docker-system-setting \
	docker-workstation

# Args:
# 1. target name
define run-docker-build
	rm -rvf $(1)/backend/target/dependency
	mkdir -v $(1)/backend/target/dependency
	cd $(1)/backend/target/dependency && unzip ../$(1)-2.10.jar
	cd $(1) && docker build -t registry.cn-qingdao.aliyuncs.com/metersphere/$(1):v2.10.20-lts .
endef

# Args:
# 1. target folder path
# 2. target name
define run-docker-build-common
	rm -rvf $(1)/target/dependency
	mkdir -v $(1)/target/dependency
	cd $(1)/target/dependency && unzip ../$(2)-2.10.jar
	cd $(1) && docker build -t registry.cn-qingdao.aliyuncs.com/metersphere/$(2):v2.10.20-lts .
endef

docker-api-test:
	$(call run-docker-build,api-test)

docker-framework-eureka:
	$(call run-docker-build-common,framework/eureka,eureka)

docker-framework-gateway:
	$(call run-docker-build-common,framework/gateway,gateway)

docker-performance-test:
	$(call run-docker-build,performance-test)

docker-project-management:
	$(call run-docker-build,project-management)

docker-report-stat:
	$(call run-docker-build,report-stat)

docker-test-track:
	$(call run-docker-build,test-track)

docker-system-setting:
	$(call run-docker-build,system-setting)

docker-workstation:
	$(call run-docker-build,workstation)


define docker-rename-image
	docker tag registry.biofan.org/$(1):v2.10.20-lts registry.cn-qingdao.aliyuncs.com/metersphere/$(1):v2.10.20-lts
endef

docker-rename-images:
	$(call docker-rename-image,api-test)
	$(call docker-rename-image,eureka)
	$(call docker-rename-image,gateway)
	$(call docker-rename-image,performance-test)
	$(call docker-rename-image,project-management)
	$(call docker-rename-image,report-stat)
	$(call docker-rename-image,test-track)
	$(call docker-rename-image,system-setting)
	$(call docker-rename-image,workstation)
